#include "stdafx.h"
#include "DlgNodeList.h"
#include "DlgCamParms.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// Dialogfeld CDlgCamParms

CDlgCamParms::CDlgCamParms(CFGCamera *pCamera,CAMERAPARMS *pCamParms,CWnd* pParent)
	: CDialog(CDlgCamParms::IDD, pParent)
{
  m_pCamera=pCamera;
  m_pCamParms=pCamParms;

	//{{AFX_DATA_INIT(CDlgCamParms)
	//}}AFX_DATA_INIT
}


void CDlgCamParms::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgCamParms)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDlgCamParms, CDialog)
	//{{AFX_MSG_MAP(CDlgCamParms)
	ON_CBN_SELCHANGE(IDC_VIDSET, OnSelchangeImageFormat)
	ON_CBN_SELCHANGE(IDC_XPOS, OnSelchangeXpos)
	ON_CBN_SELCHANGE(IDC_XSIZE, OnSelchangeXsize)
	ON_CBN_SELCHANGE(IDC_YPOS, OnSelchangeYpos)
	ON_CBN_SELCHANGE(IDC_YSIZE, OnSelchangeYsize)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// Sync combo box with values.

UINT32 CDlgCamParms::SyncCombo(UINT32 Parm,UINT32 Id,char* *ppName)
{
  CComboBox *pBox;
  UINT32     Ix,i,Sel;
  char       cbuf[128];
  FGPINFO    ParmInfo;
  UINT32     Result;

  pBox=(CComboBox*)GetDlgItem(Id);

  Result=m_pCamera->GetParameterInfo((UINT16)Parm,&ParmInfo);
  wsprintf(cbuf,"GetParameterInfo(%s)",FGParameterStr[Parm]);
  pDlgNodeList->CheckResult(cbuf,Result);
  if(Result!=FCE_NOERROR)
  {
    pBox->AddString("Error");
    pBox->SetItemData(0,(DWORD)-1);
    pBox->SetCurSel(0);
    return Result;
  }

  Sel=0;
  pBox->ResetContent();
  for(i=ParmInfo.MinValue;i<=ParmInfo.MaxValue;i+=ParmInfo.Unit)
  {
    if(!ppName)
    {
      wsprintf(cbuf,"%d",i);
      Ix=pBox->AddString(cbuf);
    }
    else
    {
      Ix=pBox->AddString(ppName[i]);
    }

    pBox->SetItemData(Ix,i);
    if(ParmInfo.IsValue==i)
     Sel=Ix;
  }
  pBox->SetCurSel(Sel);

  return FCE_NOERROR;
}

/////////////////////////////////////////////////////////////////////////////
// Sync combo box with values.

UINT32 CDlgCamParms::SetFromCombo(UINT32 Parm,UINT32 Id)
{
  CComboBox *pBox;
  char       cbuf[128];
  UINT32     Result,Value;

  wsprintf(cbuf,"SetParameter(%s)",FGParameterStr[Parm]);
  pBox=(CComboBox*)GetDlgItem(Id);
  Value=(UINT32)pBox->GetItemData(pBox->GetCurSel());
  Result=m_pCamera->SetParameter((UINT16)Parm,Value);
  pDlgNodeList->CheckResult(cbuf,Result);

  return Result;
}

/////////////////////////////////////////////////////////////////////////////
// Initialize dialog with actual settings from our camera.

BOOL CDlgCamParms::OnInitDialog()
{
  CComboBox *pBox;
  char       cbuf[256];
  char       DeviceName[128];
  UINT16     i,Cnt;
  int        Ix,n,Sel;
  UINT32     Result;
  UINT32     ImageFormat;
  FGPINFO    ParmInfo;
  UINT8      ColorMode;

  CDialog::OnInitDialog();

  // Set window title
  m_pCamera->GetDeviceName(DeviceName,sizeof(DeviceName));
  wsprintf(cbuf,"Parameters for %s",DeviceName);
  SetWindowText(cbuf);

  // Add all available modes
  pBox=(CComboBox*)GetDlgItem(IDC_VIDSET);
  
  // Reset image format enumeration and enumerate all formats
  Sel=0;
  m_pCamera->SetParameter(FGP_ENUMIMAGEFORMAT,0);
  do
  {
    Result=m_pCamera->GetParameter(FGP_ENUMIMAGEFORMAT,&ImageFormat);
    if(Result==FCE_NOERROR)
    {
      // Special entry for scalable mode
      if(IMGRES(ImageFormat)==RES_SCALABLE)
      {
        ColorMode=(UINT8)IMGCOL(ImageFormat);
        
        // Check for Vendor formats
        if(ColorMode<CM_LAST)
         wsprintf(cbuf,"Scalable Mode %d %s",IMGMODE(ImageFormat),
                  FGColorFormatStr[ColorMode]);
        else
         wsprintf(cbuf,"Scalable Mode %d Vendor Color Id %d",IMGMODE(ImageFormat),
                  ColorMode);
      }
      else
      {
        wsprintf(cbuf,"%s %s %s",
                 FGResolutionStr[IMGRES(ImageFormat)],
                 FGColorFormatStr[IMGCOL(ImageFormat)],
                 FGFrameRateStr[IMGRATE(ImageFormat)]);
      }
      
      Ix=pBox->AddString(cbuf);
      pBox->SetItemData(Ix,ImageFormat);
      
      if(ImageFormat==m_pCamParms->ImageFormat)
       Sel=Ix;
    }
  }while(Result==FCE_NOERROR);

  // Always select image format
  pBox->SetCurSel(Sel);
  
  // Set speed combo
  SyncCombo(FGP_PHYSPEED,IDC_SPEED,FGSpeedStr);

  // Set trigger combo
  pBox=(CComboBox*)GetDlgItem(IDC_TRIGGER);
  Result=m_pCamera->GetParameterInfo(FGP_TRIGGER,&ParmInfo);
  pBox->AddString("Intern");
  // When trigger register is present we always allow to switch on/off
  if(Result==FCE_NOERROR)
  {
    pBox->AddString("Extern");
    pBox->SetCurSel(ParmInfo.Specific.Data.TriggerInfo.OnOffState);
  }
  else
  {
    pBox->SetCurSel(0);
  }

  // Set invers mode
  ((CButton*)GetDlgItem(IDB_Y16INVERS))->SetCheck(m_pCamParms->Y16Invert);

  // Set actual buffer count
  pBox=(CComboBox*)GetDlgItem(IDC_BUFFERS);
  Result=m_pCamera->GetParameterInfo(FGP_FRAMEBUFFERCOUNT,&ParmInfo);
  pDlgNodeList->CheckResult("GetParameterInfo(FGP_FRAMEBUFFERCOUNT)",Result);
  Ix=0;
  for(i=1;i<=14;i+=1)
  {
    Cnt=1<<i;
    wsprintf(cbuf,"%d",Cnt);
    n=pBox->AddString(cbuf);
    pBox->SetItemData(n,Cnt);
    if(ParmInfo.IsValue==Cnt)
     Ix=n;
  }
  pBox->SetCurSel(Ix);

  // Set raw mode
  ((CButton*)GetDlgItem(IDB_YISRAW))->SetCheck(m_pCamParms->YIsRawMode);

  // Set bayerpattern
  pBox=(CComboBox*)GetDlgItem(IDC_BAYER);
  for(i=0;i<BP_LAST;i++)
   pBox->AddString(FGBayerPatternStr[(UINT8)i]);
  pBox->SetCurSel(m_pCamParms->BayerPattern);

  // Handle actual VidSet
  SyncPositionSizePacketCombo();

  // Set isochronous error handling buttons
  ((CButton*)GetDlgItem(IDB_SYNC))->SetCheck(m_pCamParms->DmaFlags&DMAF_CHECKSYNC?BST_CHECKED:0);
  ((CButton*)GetDlgItem(IDB_CYCLE))->SetCheck(m_pCamParms->DmaFlags&DMAF_CHECKCYCLE?BST_CHECKED:0);
  ((CButton*)GetDlgItem(IDB_LENGTH))->SetCheck(m_pCamParms->DmaFlags&DMAF_CHECKLENGTH?BST_CHECKED:0);

  return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// Image format selection has changed.

void CDlgCamParms::OnSelchangeImageFormat()
{
  SetFromCombo(FGP_IMAGEFORMAT,IDC_IMAGEFORMAT);
  SyncPositionSizePacketCombo();
}

/////////////////////////////////////////////////////////////////////////////
// Sync position and size combos.

void CDlgCamParms::SyncPositionSizePacketCombo()
{
  CComboBox *pBox;
  UINT32     ImageFormat;

  pBox=(CComboBox*)GetDlgItem(IDC_IMAGEFORMAT);
  ImageFormat=(UINT32)pBox->GetItemData(pBox->GetCurSel());

  // Reset all combo boxes
  ((CComboBox*)GetDlgItem(IDC_XPOS))->ResetContent();
  ((CComboBox*)GetDlgItem(IDC_YPOS))->ResetContent();
  ((CComboBox*)GetDlgItem(IDC_XSIZE))->ResetContent();
  ((CComboBox*)GetDlgItem(IDC_YSIZE))->ResetContent();
  ((CComboBox*)GetDlgItem(IDC_PACKET))->ResetContent();

  // Differ between scalable and not scalable
  if(IMGRES(ImageFormat)!=RES_SCALABLE)
  {
    // Reset all scalable parameters
    GetDlgItem(IDC_XPOS)->EnableWindow(FALSE);
    GetDlgItem(IDC_YPOS)->EnableWindow(FALSE);
    GetDlgItem(IDC_XSIZE)->EnableWindow(FALSE);
    GetDlgItem(IDC_YSIZE)->EnableWindow(FALSE);
    GetDlgItem(IDC_PACKET)->EnableWindow(FALSE);
  }
  else
  {
    // Show scalable parameters
    GetDlgItem(IDC_XPOS)->EnableWindow(TRUE);
    GetDlgItem(IDC_YPOS)->EnableWindow(TRUE);
    GetDlgItem(IDC_XSIZE)->EnableWindow(TRUE);
    GetDlgItem(IDC_YSIZE)->EnableWindow(TRUE);
    GetDlgItem(IDC_PACKET)->EnableWindow(TRUE);

    // Get position stuff
    SyncCombo(FGP_XPOSITION,IDC_XPOS);
    SyncCombo(FGP_YPOSITION,IDC_YPOS);
    SyncCombo(FGP_XSIZE,IDC_XSIZE);
    SyncCombo(FGP_YSIZE,IDC_YSIZE);
  }

  SyncCombo(FGP_PACKETSIZE,IDC_PACKET);
}

/////////////////////////////////////////////////////////////////////////////
// Adjust dialog after change of format 7 parameters.

void CDlgCamParms::OnFormat7Change()
{
  // Get/set speed
  SetFromCombo(FGP_PHYSPEED,IDC_SPEED);

  // Get/set position/size
  SetFromCombo(FGP_XSIZE,IDC_XSIZE);
  SetFromCombo(FGP_YSIZE,IDC_YSIZE);
  SetFromCombo(FGP_XPOSITION,IDC_XPOS);
  SetFromCombo(FGP_YPOSITION,IDC_YPOS);

  // Reread position (might have been changed)
  SyncCombo(FGP_XPOSITION,IDC_XPOS);
  SyncCombo(FGP_YPOSITION,IDC_YPOS);

  // Sync packet stuff
  SyncCombo(FGP_PACKETSIZE,IDC_PACKET);
}

/////////////////////////////////////////////////////////////////////////////
// Event handling.

void CDlgCamParms::OnSelchangeXpos()
{
  OnFormat7Change();
}

void CDlgCamParms::OnSelchangeYpos()
{
  OnFormat7Change();
}

void CDlgCamParms::OnSelchangeXsize()
{
  OnFormat7Change();
}

void CDlgCamParms::OnSelchangeYsize()
{
  OnFormat7Change();
}

/////////////////////////////////////////////////////////////////////////////
// Dialogfeld CDlgCamParms

void CDlgCamParms::OnOK()
{
  CComboBox *pBox;
  int        Ix;
  UINT32     Result,Size;
  UINT8      On;

  // Get image format
  pBox=(CComboBox*)GetDlgItem(IDC_IMAGEFORMAT);
  m_pCamParms->ImageFormat=(UINT32)pBox->GetItemData(pBox->GetCurSel());

  // Get speed
  m_pCamParms->PhySpeed=(UINT8)((CComboBox*)GetDlgItem(IDC_SPEED))->GetCurSel();

  // Get trigger mode
  On=(UINT8)((CComboBox*)GetDlgItem(IDC_TRIGGER))->GetCurSel();
  m_pCamParms->Trigger=MAKETRIGGER(On,
                                   TRGPOL(m_pCamParms->Trigger),
                                   TRGSRC(m_pCamParms->Trigger),
                                   TRGMODE(m_pCamParms->Trigger),
                                   TRGPARM(m_pCamParms->Trigger));

  // Get buffer cnt
  Ix=((CComboBox*)GetDlgItem(IDC_BUFFERS))->GetCurSel();
  m_pCamParms->FrameBufferCount=(UINT16)((CComboBox*)GetDlgItem(IDC_BUFFERS))->GetItemData(Ix);

  // Get Y-16 invers mode
  m_pCamParms->Y16Invert=(UINT8)((CButton*)GetDlgItem(IDB_Y16INVERS))->GetCheck();

  // Get Y-16 raw mode
  m_pCamParms->YIsRawMode=(UINT8)((CButton*)GetDlgItem(IDB_YISRAW))->GetCheck();

  // Get bayer pattern
  m_pCamParms->BayerPattern=(UINT8)((CComboBox*)GetDlgItem(IDC_BAYER))->GetCurSel();

  // Check scalable stuff
  if(IMGRES(m_pCamParms->ImageFormat)==RES_SCALABLE)
  {
    pBox=(CComboBox*)GetDlgItem(IDC_XPOS);
    m_pCamParms->XPos=(UINT16)pBox->GetItemData(pBox->GetCurSel());

    pBox=(CComboBox*)GetDlgItem(IDC_YPOS);
    m_pCamParms->YPos=(UINT16)pBox->GetItemData(pBox->GetCurSel());

    pBox=(CComboBox*)GetDlgItem(IDC_XSIZE);
    m_pCamParms->XSize=(UINT16)pBox->GetItemData(pBox->GetCurSel());

    pBox=(CComboBox*)GetDlgItem(IDC_YSIZE);
    m_pCamParms->YSize=(UINT16)pBox->GetItemData(pBox->GetCurSel());

    pBox=(CComboBox*)GetDlgItem(IDC_PACKET);
    m_pCamParms->PktSize=(UINT16)pBox->GetItemData(pBox->GetCurSel());
  }
  else
  {
    m_pCamParms->XPos=0;
    m_pCamParms->YPos=0;
    
    // Ask camera for size
    Result=m_pCamera->GetParameter(FGP_XSIZE,&Size);
    pDlgNodeList->CheckResult("GetParmeter(FGP_XSIZE)",Result);
    if(Result==FCE_NOERROR)
     m_pCamParms->XSize=(UINT16)Size;
    
    Result=m_pCamera->GetParameter(FGP_YSIZE,&Size);
    pDlgNodeList->CheckResult("GetParmeter(FGP_YSIZE)",Result);
    if(Result==FCE_NOERROR)
     m_pCamParms->YSize=(UINT16)Size;
  }

  m_pCamParms->DmaFlags&=~(DMAF_CHECKSYNC|DMAF_CHECKCYCLE|DMAF_CHECKLENGTH);
  m_pCamParms->DmaFlags|=((CButton*)GetDlgItem(IDB_SYNC))->GetCheck()?DMAF_CHECKSYNC:0;
  m_pCamParms->DmaFlags|=((CButton*)GetDlgItem(IDB_CYCLE))->GetCheck()?DMAF_CHECKCYCLE:0;
  m_pCamParms->DmaFlags|=((CButton*)GetDlgItem(IDB_LENGTH))->GetCheck()?DMAF_CHECKLENGTH:0;
  
  CDialog::OnOK();
}

